import { EnableStatus } from '@/common/entity';
import { Button, Divider, Message } from '@alifd/next';
import { useRequest } from 'ice';
import { ListVO, deleteUser, enableOrUnableUser } from '../services';

interface Props {
  row: ListVO;
  onEdit?: () => any;
  onRefresh?: () => any;
  onAuthorize?: () => any;
}

function Operation({ row, onEdit, onRefresh, onAuthorize }: Props) {
  const onSuccess = () => {
    Message.success('操作成功');
    onRefresh?.();
  };
  const { request: _delete } = useRequest<any, number[]>(deleteUser, {
    onSuccess,
  });
  const { request: enable } = useRequest(enableOrUnableUser, {
    onSuccess,
  });

  return (
    <>
      <Button type="primary" text onClick={() => _delete(row.id!)}>
        删除
      </Button>
      <Divider direction="ver" />
      <Button type="primary" text onClick={onEdit}>
        编辑
      </Button>
      <Divider direction="ver" />
      <Button type="primary" text onClick={() => enable(row.id!, !(row.status == EnableStatus.ENABLE))}>
        {row?.status == EnableStatus.ENABLE ? '禁用' : '启用'}
      </Button>
      <Divider direction="ver" />
      <Button type="primary" text onClick={() => onAuthorize?.()}>
        授权
      </Button>
    </>
  );
}

export default Operation;
